package com.xiaoleng.tlgsku.service.Impl;


import com.xiaoleng.tlgsku.dao.AttributeValueMapper;
import com.xiaoleng.tlgsku.pojo.AttributeValue;
import com.xiaoleng.tlgsku.service.AttributeValueService;
import com.xiaoleng.tlgsku.tools.Record;
import com.xiaoleng.tlgsku.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttributeValueServiceImpl implements AttributeValueService {

    @Autowired
    private AttributeValueMapper attributeValueMapper;

    @Override
    public Record findValueByKeyId(Integer attributeId) {
        Record record = new Record();
        try {
            List<String> list = attributeValueMapper.findValueByKeyId(attributeId);
            if (null != list && list.size() > 0) {
                record.setStatus(Status.SELECT_SUCCESS);
                record.setMsg("查询value成功！");
                record.setData(list);
            } else {
                record.setStatus(Status.SELECT_FALIED);
                record.setMsg("查询value失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询value异常！");
            e.printStackTrace();
        } finally {
            return record;
        }
    }

    @Override
    public Record addValue(AttributeValue attributeValue) {
        Record record = new Record();
        int count = 0;
        try {
            count = attributeValueMapper.addValue(attributeValue);
            if (count > 0) {
                record.setStatus(Status.SAVE_SUCCESS);
                record.setMsg("添加规格value成功！");
            } else {
                record.setStatus(Status.SAVE_FALIED);
                record.setMsg("添加规格value失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("添加规格value异常！");
            e.printStackTrace();
        } finally {
            record.setData(count);
            return record;
        }
    }

    @Override
    public Record delete(Integer goodsId) {
        Record record = new Record();
        int count = 0;
        try {
            count = attributeValueMapper.delete(goodsId);
            if (count > 0) {
                record.setStatus(Status.REMOVE_SUCCESS);
                record.setMsg("删除规格成功！");
            } else {
                record.setStatus(Status.REMOVE_FALIED);
                record.setMsg("删除规格失败！");
            }
        } catch (Exception e) {
            record.setStatus(Status.REMOVE_FALIED);
            record.setMsg("删除规格异常！");
            e.printStackTrace();
        } finally {
            record.setData(count);
            return record;
        }
    }

//    @Override
//    public void modidy(String attributeValue, Integer id) {
//        attributeValueMapper.modidy(attributeValue,id);
//    }
}
